package cn.kgc.controller;

import cn.kgc.pojo.Organization;
import cn.kgc.pojo.Position_yy;
import cn.kgc.pojo.Resume_yy;
import cn.kgc.service.PositionService_yy;
import cn.kgc.service.ResumeService_yy;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author: yy
 * @date: 2022/2/19
 * @description:
 */
@Controller
public class JianLiController_yy {

    @Resource
    private PositionService_yy ps;

    @Resource
    private ResumeService_yy rs;

    @RequestMapping("/selyy")
    public String sel() {
        return "jianLiSel";
    }

    @ResponseBody
    @RequestMapping("/selyyy/sel")
    public String sel0(@RequestParam String name, @RequestParam String experience,
                       @RequestParam String positionName, @RequestParam String status,
                       @RequestParam String rTimes,
                       @RequestParam Integer currentPage, @RequestParam Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Resume_yy> list = rs.selRyy(name, positionName, experience, status, rTimes);
        PageInfo<Resume_yy> pageInfo = new PageInfo<Resume_yy>(list);
        return JSON.toJSONStringWithDateFormat(pageInfo, "yyyy-MM-dd");
    }


    @RequestMapping("/addyy")
    public String add() {
        return "JianLiAdd";
    }

    @RequestMapping("zhiwei")
    @ResponseBody
    public String add2() {
        List<Organization> list = ps.selOyy();
        return JSONObject.toJSONString(list);
    }

    @RequestMapping("/zhiweisel")
    @ResponseBody
    public String add3(@RequestParam(required = false) Integer positionid,
                       @RequestParam(required = false) String name,
                       @RequestParam(required = false) Integer departmentid,
                       @RequestParam(required = false) Integer currentPage,
                       @RequestParam(required = false) Integer pageSize) {

        PageHelper.startPage(currentPage, pageSize);
        Page<Position_yy> list = ps.selPyy(positionid, name, departmentid);
        PageInfo<Position_yy> pageInfo = new PageInfo<Position_yy>(list);
        return JSON.toJSONString(pageInfo);
    }

    @ResponseBody
    @RequestMapping("xuanze")
    public String add4(Integer positionid) {
        Position_yy position = ps.chaPyy(positionid);
        System.out.println(position);
        return JSON.toJSONString(position);
    }



    @RequestMapping("/addyy1")
    //multipart
    public String add1(MultipartFile multipartFile, @ModelAttribute Resume_yy resume,Model model) {
     String status="";

            String oldFilename = multipartFile.getOriginalFilename();//上传文件名
            String suffix = FilenameUtils.getExtension(oldFilename);//文件后缀名
        if (suffix.equalsIgnoreCase("pdf")||suffix.equalsIgnoreCase("doc")) {
            System.out.println("=============================================="+resume);
            System.out.println(resume.getIsInterview()+"======================");
            if (!resume.getIsInterview().equals("是")) {
                status = "存档";
                resume.setReferrer("");
                resume.setTime(null);
                resume.setComment("");
            } else {
                status = "推荐面试";
            }
            boolean b = rs.addRyy(multipartFile, resume, status);
            return "jianLiSel";
        }else {
            model.addAttribute("cuowu","格式不正确，格式应为pdf或doc!" );
            return "JianLiAdd";
        }
    }
    @RequestMapping("xinxi/{resumeId}")
    public String xinxi(@PathVariable Integer resumeId,Model model){
        Resume_yy re= rs.chaRyy(resumeId);
        model.addAttribute("jl",re);
        return "jianLiMX";
    }

    @GetMapping("mingXi/{resumeId}")
    public void mingXi(@PathVariable Integer resumeId, HttpServletResponse response) throws IOException {

        System.out.println("===================================================进入查看文件");
        Resume_yy re= rs.chaRyy(resumeId);

//        // 获取文件路径
        System.out.println("File: " + re);
        String servrepath = System.getProperty("user.dir")+"\\src\\main\\resources\\upload\\";
        // 获取文件输入流（把磁盘上的文件通过IO加载到程序（内存）中


            FileInputStream fis = new FileInputStream(new File(servrepath,re.getFileName()));

            //附件下载
        // 注意：为防止中文乱码，需要使用UTF-8
        response.addHeader("content-disposition", "inline;fileName=" + URLEncoder.encode(re.getFileName(), "UTF-8"));

                    // 获取响应流（找到后需要通过Response发送回给用户
            ServletOutputStream os = response.getOutputStream();

            // 文件拷贝
        IOUtils.copy(fis, os);

        // 关闭流
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(os);
    }


    @RequestMapping("xiu/{resumeId}")
    public String xiugai(@PathVariable Integer resumeId,Model model){
        Resume_yy re = rs.chaRyy(resumeId);
        model.addAttribute("jianLi",re);
        return "jianLiXiu";
    }

    @RequestMapping("xiugai")
    public String xiu(MultipartFile multipartFile,Resume_yy resume){

        System.out.println(resume+"++++++++++++++++++++++++++++++++++++++++++++");
        String oldFilename = multipartFile.getOriginalFilename();//上传文件名
        String suffix = FilenameUtils.getExtension(oldFilename);//文件后缀名
        if (suffix.equalsIgnoreCase("pdf")||suffix.equalsIgnoreCase("docx")) {

            boolean b = rs.xiuRyy(multipartFile,resume);
            return "jianLiSel";
        }else {
            return "JianLiXiu";
        }
    }
}
